var data1 = [{'id':'1','manufacturername':'text1','purchaseorderno':'text2','contracttype':'text3','contractunit':'text4','cdate':'text5','ddate':'text6','placeofdelivery':'text7'},{'id':'2','manufacturername':'text1','purchaseorderno':'text2','contracttype':'text3','contractunit':'text4','cdate':'text5','ddate':'text6','placeofdelivery':'text7'},
    {'id':'3','manufacturername':'text1','purchaseorderno':'text2','contracttype':'text3','contractunit':'text4','cdate':'text5','ddate':'text6','placeofdelivery':'text7'},{'id':'4','manufacturername':'text1','purchaseorderno':'text2','contracttype':'text3','contractunit':'text4','cdate':'text5','ddate':'text6','placeofdelivery':'text7'}];

var data2 = [{'id':'1','manufacturername':'text1','purchaseorderno':'text2','contracttype':'text3','contractunit':'text4','cdate':'text5','ddate':'text6','placeofdelivery':'text7'},{'id':'2','manufacturername':'text1','purchaseorderno':'text2','contracttype':'text3','contractunit':'text4','cdate':'text5','ddate':'text6','placeofdelivery':'text7'},
    {'id':'3','manufacturername':'text1','purchaseorderno':'text2','contracttype':'text3','contractunit':'text4','cdate':'text5','ddate':'text6','placeofdelivery':'text7'},{'id':'4','manufacturername':'text1','purchaseorderno':'text2','contracttype':'text3','contractunit':'text4','cdate':'text5','ddate':'text6','placeofdelivery':'text7'}];

var layer = layui.layer //弹层
    ,table = layui.table //表格
    form = layui.form,$ = layui.jquery,changeList=[],laydate = layui.laydate,indexs = 0,checkboxs =false;
var billNO="";//合同号
var productList=[];//产品列表
var waixiaohetong={};//外销合同

var waixiaohetongProductList=[];//外销合同产品列表
lay('.starttime').each(function(){
    laydate.render({
        elem: this
    });
});
table.render({
    elem: '#mains'
    //,data:data1
        ,url:"exportContract/import/getContractByPage"
        ,where: {
            "billNo":$("input[name='billNo']")[0].value,
            "customerContactNo":$("input[name='customerContactNo']")[0].value,
            "signingDateStart":$("input[name='signingDateStart']")[0].value,
            "signingDateEnd":$("input[name='signingDateEnd']")[0].value,
            "customerName":$("input[name='customerName']")[0].value,
            "isDeclare":$("select[name='isDeclare']")[0].value

        }
        ,method:"get"
    ,cols: [[
        {checkbox: true, sort : true,title:'选择'}
        ,{field:'billNo', title:'合同编号',sort:true}
        ,{field:'customerCode', title:'客户合同号',sort:true}
        ,{field:'customerName', title:'客户名称',sort:true}
        ,{field:'signingDate', title:'签约日期',sort:true}
        ,{field:'leadTime', title:'备货期限',sort:true}
        ,{field:'carryTime', title:'出运日期',sort:true}
        ,{field:'modeOfTransport', title:'运输方式',sort:true}
        ,{field:'salesmanCode', title:'业务员',sort:true}
        ,{field:'businessModelName', title:'业务模式',sort:true}
    ]]
    ,id: 'mainstableid'
    ,height: 300
    ,page: true
    ,done: function (res, curr, count) {
        waixiaohetong=res.data[0];
        table.render({
            elem: '#product'
            ,method:'get'
            ,url:'exportContract/import/getContractProductByPage'
            ,where: {
                id:res.data.length == 0 ? "":res.data[0].id //默认第一条信息，数据为空则id为空
            }
            //,data:data2
            ,cols: [[
                {checkbox: true, sort : true,title:'选择'}
                ,{field:'productCode', title:'产品编码', sort : true}
                ,{field:'specificationCode', title:'规格编码', sort : true}
                ,{field:'specificationExplanation', title:'规格说明', sort : true}
                ,{field: 'clientProductNo', title: '客户货号', sort: true}
                ,{field:'productCname', title:'中文名称', sort : true}
                ,{field:'productEname', title:'英文名称', sort : true}
                ,{field: 'productCdesc', title: '中文描述', sort: true}
                ,{field: 'productEdesc', title: '英文描述', sort: true}
                ,{field:'productNum', title:'数量', sort : true}
                ,{field:'productUnitCode', title:'单位', sort : true}
                ,{field:'productSurplusNum', title:'剩余数量', sort : true}
                ,{field: 'unitPrice', title: '单价', sort : true}
                ,{field: 'moneySubtotal', width: 120, title: '金额小计', sort : true}
            ]]
            ,id: 'producttableid'

            ,height: 400
            ,done:function () {
                $("th .layui-table-cell.laytable-cell-checkbox").html("选择");
            }
        });
       /* if(res.data.length != 0){
            var jsons = {"id":res.data[0].id,"changeList":[]}
            changeList.push(jsons);
        }*/
    }
});

//监听复选框
table.on('checkbox(mainstable)', function (data) {

    checkboxs = true;
    table.reload('producttableid', {
        data:data1
        ,where: data.data.id //点击行ID
        ,done: function (res, curr, count) {
            var shu = 0;
            for(var i = 0;i<changeList.length;i++){
                if(changeList[i].id == data.data.id){
                    indexs = i;
                    shu ++;
                }
            }
            if(shu == 0){
                var jsons = {"id":data.data.id,"changeList":[]};
                changeList.push(jsons);
                indexs = changeList.length-1;
            }
            var child = $("#product").next().find('tbody input[type="checkbox"]');
            child.each(function(index, item){
                item.checked = data.checked;
            });
            if(data.checked){　　　　　　//判断当前多选框是选中还是取消选中
                changeList[indexs].changeList = layui.table.cache["producttableid"];
            }else{
                changeList[indexs].changeList = [];
            }
            $("th .layui-table-cell.laytable-cell-checkbox").html("选择");
            form.render('checkbox');
            layer.closeAll();    //返回数据关闭loading
        }
    }, 'data');
});

//监听产品复选框
table.on('checkbox(producttable)', function (obj) {
    if(obj.checked){
        obj.data.billNO=billNO;
        productList.push(obj.data);
    }else{
        for(var i=0;i<productList.length;i++){
            if(productList[i].id==obj.data.id){
                productList.splice(i,1);
                return;
            }
        }
    }
   /* var index = layer.load(1); //添加laoding
    //如果checked为false
    if (!obj.checked) {
        if(obj.type=="all"){
            changeList[indexs].changeList = [];
        }else{
            for (var i = 0; i < changeList[indexs].changeList.length; i++) {
                //如果数据中有这个数据就移除
                if (changeList[indexs].changeList[i].id == obj.data.id) {
                    changeList[indexs].changeList.splice(i, 1);
                }
            }
        }
    }else{
        if(obj.type=="all"){
            changeList[indexs].changeList = layui.table.cache["producttableid"];
        }else{
            //如果没有这个数据就加进去
            changeList[indexs].changeList.splice(changeList[indexs].changeList.length, 0, obj.data);
        }
    };

    var child = $("#mains").next().find('tbody tr');
    //input[type="checkbox"]
    var mainsdata = layui.table.cache["mainstableid"];
    child.each(function(index, item){
        var dataindex =  $(item).attr("data-index");
        if(mainsdata[dataindex].id == changeList[indexs].id && $(item).find('td input[type="checkbox"]')[0].checked != true){
            $(item).find('td input[type="checkbox"]')[0].checked = true;
        }
    });
    // form.render('checkbox');
    table.reload('producttableid', {
        data:data1
        ,where: changeList[indexs].id//点击行ID
        ,done: function (res, curr, count) {
                var subscript = [];
                for(var i = 0 ;i<res.data.length;i++){
                    for(var j = 0;j<changeList[indexs].changeList.length;j++){
                        if(res.data[i].id == changeList[indexs].changeList[j].id){
                            var k = i;
                            subscript.push(k);
                        }
                    }
                }
                var child = $("#product").next().find('tbody input[type="checkbox"]');
                child.each(function(index, item){
                    var shul = 0;
                    for(var i = 0 ;i<subscript.length;i++){
                        if(index == subscript[i]){
                            shul++;
                        }
                    }
                    if(shul == 0){
                        item.checked = false;
                    }else{
                        item.checked = true;
                    }
                });
            form.render('checkbox');
            $("th .layui-table-cell.laytable-cell-checkbox").html("选择");
            layer.close(index);    //返回数据关闭loading
        }
    }, 'data');*/
});

//单击行事件
table.on('row(mainstable)', function (obj) {
    waixiaohetong=obj.data;
    billNO=obj.data.billNo;
            table.reload('producttableid', {
                //data:data1
                method:'get'
                ,url:'exportContract/import/getContractProductByPage'
                ,where: {
                    id: obj.data.id //默认第一条信息，数据为空则id为空
                 }

                ,done: function (res, curr, count) {

                }
            }, 'data');


});
//搜索
form.on('submit(submitform)', function(data){
    //layer.msg(JSON.stringify(data.field));
        table.reload('mainstableid', {
            url:"exportContract/import/getContractByPage"
            ,where: {
            "billNo":$("input[name='billNo']")[0].value,
                "customerContactNo":$("input[name='customerContactNo']")[0].value,
                "signingDateStart":$("input[name='signingDateStart']")[0].value,
                "signingDateEnd":$("input[name='signingDateEnd']")[0].value,
                "customerName":$("input[name='customerName']")[0].value,
                "isDeclare":$("select[name='isDeclare']")[0].value
        }
       ,method:"get"
        ,page: {
            curr: 1 //重新从第 1 页开始
        }

    }, 'data');
    return false;
});

$("#choose").on("click",function () {
    /*var purchaseorderdata =   table.checkStatus("mainstableid").data; //第一个表格选中的数据
    if(purchaseorderdata.length + changeList.length == 0){
        layer.msg("请选择数据")
    }else{
        //执行接口
        layer.msg("保存成功", {icon: 6});
        setTimeout(function () {
            //保存成功则关闭此页面
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        },1000)
    }*/
   // window.sessionStorage.setItem("productList",JSON.stringify(productList));
    parent.cyjhresh(productList,waixiaohetong);
    setTimeout(function () {
        //保存成功则关闭此页面
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    },1000)
})
$("#shutdown").on("click",function () {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
})